package com.obtk.service.impl;

import com.obtk.mapper.CstLinkmanMapper;
import com.obtk.mapper.CstServiceMapper;
import com.obtk.mapper.CustomerMapper;
import com.obtk.model.CstLinkman;
import com.obtk.model.CstService;
import com.obtk.model.Customer;
import com.obtk.model.SelChance;
import com.obtk.service.CstLinkmanService;
import com.obtk.service.CstServiceService;
import com.obtk.service.SelChanceService;
import com.obtk.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-04-06 09:27:59
 */
@Service
public class CstLinkmanServiceImpl implements CstLinkmanService {
    @Autowired
    private CstLinkmanMapper cstLinkmanMapper;
    @Autowired
    private SelChanceService chanceService;
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public List<CstLinkman> selectCstLinkman(CstLinkman cstLinkman) {
        return cstLinkmanMapper.selectCstLinkman(cstLinkman);
    }

    @Override
    public CstLinkman selectByLkmName(String lkmName) {
        return cstLinkmanMapper.selectByLkmName(lkmName);
    }
    //    新增一条数据 但是会查询一次数据库是否有相同电话
    @Override
    public CstLinkman selectByLkmMobile(String lkmMobile) {
        return cstLinkmanMapper.selectByLkmMobile(lkmMobile);
    }

    @Override
    @Transactional
    public int register(CstLinkman cstLinkman, SelChance selChance,Customer customer) {
        String uuId = UUIDUtil.getUniqueIdByUUId();
        cstLinkman.setLkmId(uuId);
        cstLinkman.setLkmStatus(1);
        cstLinkman.setLkmBirthday(new Date());
        System.out.println("联系人表数据:"+cstLinkman);
        System.out.println("客户编号:" + cstLinkman.getLkmCustId());//客户编号
        System.out.println("联系人姓名:" + cstLinkman.getLkmName());//联系人姓名
        System.out.println("联系人电话:" + cstLinkman.getLkmMobile());//联系人电话
        System.out.println("联系人状态:" + cstLinkman.getLkmMemo());//联系人状态
        //      前往客户表获取客户名
        customer.setCustId(cstLinkman.getLkmCustId());
        System.out.println("wx:"+customer.getCustId());
        System.out.println("孙伟3"+customer);

        Customer customers = customerMapper.selectCustId(customer);
        System.out.println("123"+customers.getCustId());
        /** 新增一条销售机会数据*/
        selChance.setChcSource(cstLinkman.getLkmMemo());//联系人状态
        selChance.setChcRate(1);//成功机率
        selChance.setChcTitle("12345678912");//公司座机
        selChance.setChcCreateId(2);
        selChance.setChcLinkman(cstLinkman.getLkmName());
        selChance.setChcCreateName("孙伟");
        selChance.setChcCreateDate(new Date());
        selChance.setChcCustName(customers.getCustName());//客户姓名
        selChance.setChcStatus(2);
        selChance.setChcDueName("张儒");
        selChance.setChcDueId(4);
        selChance.setChcCreateDate(new Date());
        selChance.setChcDueStatus(1);
        selChance.setChcDesc("相谈甚欢");//成功机率
        selChance.setChcTel(cstLinkman.getLkmMobile());//联系人电话
        selChance.setChcSource(cstLinkman.getLkmMemo());//联系人来源
        selChance.setChcBirthday(cstLinkman.getLkmBirthday()); // 联系人生日
        selChance.setChcGender(cstLinkman.getLkmGender()); // 联系人 性别
        selChance.setChcId(cstLinkman.getLkmId()); // 联系人编号
        System.out.println("孙伟1"+cstLinkman);
        System.out.println("孙伟2"+selChance);
        System.out.println("孙伟3"+customer);
        System.out.println("是否有名字:"+selChance.getChcCustName());
        System.out.println("销售机会表数据"+selChance);
        int register = chanceService.register(selChance);
        int insert = cstLinkmanMapper.insert(cstLinkman);
        if (register == insert){
            return 1;
        }else {
            return 0;
        }

    }

    @Override
    public CstLinkman selectByLkmId(Integer ids) {
        return cstLinkmanMapper.selectByLkmId(ids);
    }

    @Override
    public int updateByCstLinkmanId(CstLinkman cstLinkman) {
        cstLinkman.setLkmBirthday(new Date()); // 更改生日事件
        return cstLinkmanMapper.updateByCstLinkmanId(cstLinkman);
    }

    @Override
    public int updateStatusBatch(List<String> ids, Integer lkmStatus) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("ids", ids);
        params.put("lkmStatus", lkmStatus);
        return cstLinkmanMapper.updateStatusBatch(params);
    }

    @Override
    public int updateLkmName(String lkmName, Integer lkmStatus) {
        return cstLinkmanMapper.updateLkmName(lkmName,lkmStatus);
    }
}
